package com.mediacloud.app.newsmodule.fragment.baoliao.api;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;
import com.mediacloud.app.reslib.util.HTTPClient;
import com.mediacloud.app.user.net.JSONObjectConvertFactory;
import com.mediacloud.app.user.net.SSLSocketClient;
import com.mediacloud.app.user.net.SignInterceptor;
import com.mediaclound.appfactnet.utils.GETBadInterceptor;
import com.mediacloud.app.util.ProxyUtil;

import org.json.JSONObject;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.X509TrustManager;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;

/**
 * @author TXL
 * description :
 */
public class AiAssistantApi {

   // private volatile static ISpiderCmsApi spiderCmsApi;
    private volatile static ISpiderCmsApi spiderCmsApiJson;

    /*public static ISpiderCmsApi getSpiderCmsApi(Context context) {
        if (spiderCmsApi == null) {
            synchronized (AiAssistantApi.class) {
                if (spiderCmsApi == null) {
                    initSpiderCmsApi(context.getApplicationContext());
                }
            }
        }
        return spiderCmsApi;
    }*/

    /*private static void initSpiderCmsApi(Context context) {
        String baseUrl = AppFactoryGlobalConfig.getAppServerConfigInfo(context).robot;
        if (TextUtils.isEmpty(baseUrl)) {
            Log.e("DataInvokeUtil", "initSpiderCmsApi failed spider_cms is null");
            return;
        }
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.dns(new MyDns());
        ProxyUtil.config(builder);
        builder.addInterceptor(new GETBadInterceptor(AppFactoryGlobalConfig.getAppServerConfigInfo(context).robot,AppFactoryGlobalConfig.getAppServerConfigInfo(context).cdn_robot));
        builder.protocols(Collections.unmodifiableList(Arrays.asList(Protocol.HTTP_1_1, Protocol.HTTP_2)));
        builder.addInterceptor(new SignInterceptor());
        builder.addInterceptor(interceptor);
        builder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), (X509TrustManager) SSLSocketClient.getTrustManager()[0]);
        builder.hostnameVerifier(SSLSocketClient.getHostnameVerifier());
        builder.readTimeout(HTTPClient.TIME_OUT, TimeUnit.SECONDS);
        builder.connectTimeout(HTTPClient.TIME_OUT, TimeUnit.SECONDS);
        builder.addInterceptor(chain -> {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .header("tenantId", context.getResources().getString(com.mediacloud.app.reslib.R.string.tenantid))
                    .build();
            return chain.proceed(request);
        });
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl+"/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(builder.build())
                .build();
        spiderCmsApi = retrofit.create(ISpiderCmsApi.class);
    }*/

    public static ISpiderCmsApi getSpiderCmsApiJson(Context context) {
        if (spiderCmsApiJson == null) {
            synchronized (AiAssistantApi.class) {
                if (spiderCmsApiJson == null) {
                    initSpiderCmsApiJson(context);
                }
            }
        }
        return spiderCmsApiJson;
    }

    private static void initSpiderCmsApiJson(Context context) {
        String baseUrl = AppFactoryGlobalConfig.getAppServerConfigInfo(context).robot;
        if (TextUtils.isEmpty(baseUrl)) {
            Log.e("DataInvokeUtil", "initSpiderCmsApi failed spider_cms is null");
            return;
        }
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(new GETBadInterceptor(AppFactoryGlobalConfig.getAppServerConfigInfo(context).robot,AppFactoryGlobalConfig.getAppServerConfigInfo(context).cdn_robot));
        builder.addInterceptor(new SignInterceptor());
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
        builder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), (X509TrustManager) SSLSocketClient.getTrustManager()[0]);
        builder.hostnameVerifier(SSLSocketClient.getHostnameVerifier());
        builder.readTimeout(HTTPClient.TIME_OUT, TimeUnit.SECONDS);
        builder.connectTimeout(HTTPClient.TIME_OUT, TimeUnit.SECONDS);
        ProxyUtil.config(builder);
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl+"/")
                .addConverterFactory(new JSONObjectConvertFactory())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(builder.build())
                .build();
        spiderCmsApiJson = retrofit.create(ISpiderCmsApi.class);
    }

    public interface ISpiderCmsApi {

        /**
         * 开始会话
         *
         * @param clientId
         * @return
         */
        @GET("api/faqRobot/getChatSp")
        Observable<JSONObject> startASession(@Query("clientId") String clientId, @QueryMap Map<String, Object> map);


        @GET("api/faqRobot/questionAnswer")
        Observable<JSONObject> chatbot(@Query("clientId") String clientId, @Query("question") String question,  @Query("tenantId") String tenantId,  @Query("pageRecognition") int pageRecognition, @QueryMap Map<String, Object> map);

        @GET("api/spider/number-info/{id}")
        Observable<JSONObject> getIpAddress(@Path("id") String user_id, @Query("access_token") String access_token, @Query("is_member_id") int is_member_id);


    }
}